package com.xykj.enterprise.wechat.ext.api.identity;

import com.xykj.enterprise.wechat.bean.ext.identity.JsapiTicketRsp;
import com.xykj.enterprise.wechat.ext.service.identity.IdentityService;
import com.xykj.enterprise.wechat.util.biz.BizException;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author zhouxu
 * @create 2021-02-23 11:46
 */
@Slf4j
@Function(description = "获取应用的jsapi_ticket", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),
})
@Component
public class GetAgentJsapiTicket implements Action {
    @Autowired
    private IdentityService identityService;

    @Override
    public Response execute(Request request) throws Exception {
        try {

            JsapiTicketRsp rsp = identityService.getAgentJsapiTicket(
                    request.getString("access_token")
            );
            return Response.success().put("data", rsp);

        } catch (BizException biz) {
            log.error(biz.getErrMsg(), biz.getErrCode());
            return Response.fail(biz.getMessage(), biz.getErrCode());
        } catch (Exception e) {
            log.error("AccessToken 获取失败", e);
            return Response.fail(e);
        }

    }
}
